package com.yami.shop.search.common.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.yami.shop.bean.model.UserSearchHistory;
import com.yami.shop.bean.param.EsProductParam;
import com.yami.shop.bean.vo.search.EsProductSearchVO;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.i18n.LanguageEnum;
import com.yami.shop.common.util.EsPageVO;
import com.yami.shop.search.common.constant.EsConstant;
import com.yami.shop.search.common.constant.EsIndexEnum;
import com.yami.shop.search.common.constant.EsProductSortEnum;
import com.yami.shop.search.common.param.EsPageParam;
import com.yami.shop.search.common.service.EsHotWordsService;
import com.yami.shop.search.common.util.EsSearchHotWordUtil;
import com.yami.shop.search.common.util.EsSearchProdUtil;
import com.yami.shop.search.common.util.EsSearchUtil;
import com.yami.shop.search.common.util.SearchResponseUtil;
import com.yami.shop.service.UserSearchHistoryService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.common.lucene.search.function.CombineFunction;
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FieldValueFactorFunctionBuilder;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author Yami
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class EsHotWordsServiceImpl implements EsHotWordsService {

    private final UserSearchHistoryService userSearchHistoryService;

    @Override
    public void save(Long prodId) {
        UserSearchHistory byId = userSearchHistoryService.getById(prodId);
        if (Objects.isNull(byId)) {
            return;
        }
        EsSearchHotWordUtil.save(byId);
    }
    @Override
    public void deleteBatch(List<Long> ids) {
        List<String> idstr = new ArrayList<String>();
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        ids.forEach(s->{
            idstr.add(String.valueOf(s));
        });
        EsSearchHotWordUtil.deleteBatch(idstr);
    }

}
